package com.zygk.locationtool.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.zygk.library.adapter.BaseListAdapter;
import com.zygk.locationtool.R;
import com.zygk.locationtool.model.M_Station;
import java.util.List;

/**
 * 站点列表适配器
 */
public class StationSearchAdapter extends BaseListAdapter<M_Station> {

  public StationSearchAdapter(Context context, List<M_Station> list) {
    super(context, list);
  }

  @Override public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
      convertView = mInflater.inflate(R.layout.item_site_list_content, null, false);
      holder = new ViewHolder(convertView);

      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    final M_Station m = getItem(position);

    holder.tvStationName.setText(m.getStationName());
    holder.tvState.setVisibility(m.isHasSet() ? View.GONE : View.VISIBLE);

    holder.llRoot.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View view) {
        if (onItemClickListener != null) {
          onItemClickListener.onItemClick(m);
        }
      }
    });
    return convertView;
  }

  public interface onItemClickListener {
    void onItemClick(M_Station station);
  }

  private onItemClickListener onItemClickListener;

  public void setOnItemClickListener(
      onItemClickListener onItemClickListener) {
    this.onItemClickListener = onItemClickListener;
  }

  static class ViewHolder {
    @BindView(R.id.tv_station_name) TextView tvStationName;
    @BindView(R.id.tv_state) TextView tvState;
    @BindView(R.id.ll_root) LinearLayout llRoot;

    ViewHolder(View view) {
      ButterKnife.bind(this, view);
    }
  }
}
